$(function () {
    $container = pageManager.$container;

    $container.on('keyup', '.weui-search-bar__input', function (e) {
        var self = $(this);
        if (e.keyCode == "13") {
            var value = self.val();
            var key = self.attr("name");
            var paraMap = new Map();
            if (key.indexOf('|') != -1) {
                var keys = key.split('|');
                for (var i = 0; i < keys.length; i++) {
                    paraMap.set(keys[i], value);
                }
            } else {
                paraMap.set(key, value)
            }
            pageManager.updatePara(paraMap);
            var name = pageManager._pageStack[pageManager._pageStack.length - 1].config.name;
            pageManager._refresh(name);
        }
    });

    $container.on("click", ".weui-search-detail__btn", function () {
        var form = $(this).closest(".weui-popup__modal").find("form")[0];
        var paraMap = new Map();
        var formArray = $(form).serializeArray();
        $.each(formArray, function (i, item) {
            paraMap.set(item.name, item.value);
        });
        pageManager.updatePara(paraMap);
        var name = pageManager._pageStack[pageManager._pageStack.length - 1].config.name;
        pageManager._refresh(name);
    });

    $container.on("click", ".open-qrscan", function () {
        var self = this;
        wx.scanQRCode({
            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
            scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
            success: function (res) {
                var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                result = result.substring(result.indexOf(",") + 1);
                var key = $(self).data("name");
                var paraMap = new Map();
                if (key.indexOf('|') != -1) {
                    var keys = key.split('|');
                    for (var i = 0; i < keys.length; i++) {
                        paraMap.set(keys[i], result);
                    }
                } else {
                    paraMap.set(key, result)
                }
                pageManager.updatePara(paraMap);
                var name = pageManager._pageStack[pageManager._pageStack.length - 1].config.name;
                pageManager._refresh(name);
            }
        });
    });

    $container.on("click", ".weui-cell__add", function () {
        var self = this;
        var html = $(self).parent().find(".weui-cell__add_content").html();
        $(self).parent().find(".weui-cell__add_target").append(html);
    });

    var MAX = 99, MIN = 1;
    $container.on("click", ".weui-count__decrease", function () {
        var $input = $(this).parent().find('.weui-count__number');
        var number = parseInt($input.val() || "0") - 1;
        if (number < MIN) number = MIN;
        $input.val(number);
        $input.attr("value",number);
    });

    $container.on("click", ".weui-count__increase", function () {
        var $input = $(this).parent().find('.weui-count__number');
        var number = parseInt($input.val() || "0") + 1;
        if (number > MAX) number = MAX;
        $input.val(number);
        $input.attr("value",number);
    });
})